<?php
import("ORG.Util.Function");
import("@.Org.Utils");

class TypeAction extends CommonAction{	
	
	public function _initialize(){
		parent::_initialize();				
		$param           =    $this->_get('_URL_');
		$controller      =    $param[0];
		$action          =    $param[1];		
		//language
		$lang            =    L($action);  		
		$manage          =    L('manage');
		
		$this->assign("manage",$manage);
		$this->assign("current",$lang);
		$this->assign("lang",$lang);
	}
	
	public function _empty(){
		t('空操作');
	}
	
	// 商品类型列表
	public function type_list(){		
		$TypeModel	=	new TypeModel();
		$typeList	=	$TypeModel->getTypeList();
		$this->assign("typeList",$typeList);	
		$this->display();
	}
	
	// 添加商品类型
	public function add_type(){
		$this->display();
	}
	
	// 处理添加商品类型
	public function add_type_ok(){	
		
		$goods_type['cat_name']		=	Utils::sub_str($_POST['cat_name'], 60);
		$goods_type['attr_group']	=	Utils::sub_str($_POST['attr_group'], 255);
		$goods_type['enabled']		=	intval($_POST['enabled']);
		// 判断是否有重复
		$cat_id						=	M('GoodsType')->where(" cat_name='{$goods_type['cat_name']}'")->getField("cat_id");
		if($cat_id){
			$this->error("已存在该类型,正在为您跳转上一次页面...",U('type/add_type'));
		}
		// 入库
		if( M('GoodsType')->add($goods_type) ){
			$this->success("操作成功,正在为您跳转...",U('type/type_list'));
		}
		else{
			$this->success("操作失败,正在为您跳转...",U('type/type_list'));
		}
	}
	
	// 删除商品类型
	public function del_type(){
		$id 		= 	intval($_GET['id']);		
		$name		=	M('GoodsType')->field("cat_name")->where("cat_id = '{$id}'")->getField("cat_name");
				
		$result		=	M('GoodsType')->where("cat_id='{$id}'")->delete();		
		if($result){
			// 清除该类型下的所有属性
			$attr	=	M('Attribute')->field("attr_id")->where("cat_id = '{$id}'")->select();
			
			$sql	=	" delete from ybr_attribute where attr_id "  . Utils::create_in_sql($attr);			
			M('Attribute')->query($sql);
			$sql	=	" delete from ybr_goods_attr where attr_id "  . Utils::create_in_sql($attr);
			M('GoodsAttr')->query($sql);
			// 跳转页面
			$this->error("操作成功",U('type/type_list'));
		}
		else{
			$this->error("操作失败",U('type/type_list'));
		}
	}
	
	// 修改商品类型
	public function mod_type(){
		
		$this->display();
	}
	
	// 商品回收站
	public function goods_trash(){
		
	}	
		
}
?>